<?php

/*
 * ************************************************* 
 * Created on :2012-5-2 15:25:21
 * Encoding   :UTF-8
 * Description:
 * 
 * @Author @lqs <soso2501@gmail.com>
 * @WAP_WEIBO (C)1996-2099 SINA Inc. 
 * ************************************************
 */

class Photo {

    static $allow_project = array('捐赠相册' => 'donate', '讲堂相册' => 'lecture', '校庆相册' => 'celebration');
    static $allow_category = array("photo", "vcr");
    static $albumtable = 'album';
    static $phototable = 'photo';

    public static function addalbum($name, $describe, $project, $category, $remark, $cover) {
        if (!in_array($project, self::$allow_project) ||
                !in_array($category, self::$allow_category) ||
                empty($name) || empty($describe))
            return false;
        $data = array("name" => $name, "describe" => $describe, "project" => $project,
            "category" => $category, "createtime" => date("Y-m-d H:i:s"),
            "userid" => $GLOBALS['user']->userid, "remark" => $remark, "cover" => $cover);
        return DB::Insert(self::$albumtable, $data);
    }

    public static function addphoto($describe, $path, $thumb, $albumid) {
        if ( empty($path) || empty($albumid))
            return false;
        $data = array("describe" => $describe, "path" => $path, "thumb" => $thumb,
            "albumid" => $albumid, "createtime" => date("Y-m-d H:i:s"),
            "userid" => $GLOBALS['user']->userid);
        return DB::Insert(self::$phototable, $data);
    }

    public static function delalbum($albumid) {
        if (empty($albumid))
            return false;
        return DB::Delete(self::$albumtable, array("albumid" => $albumid));
    }

    public static function delphoto($photoid) {
        if (empty($photoid))
            return false;
        return DB::Delete(self::$phototable, array("photoid" => $photoid));
    }

    public static function upload($sourcename, $ext) {
        $public_dir = ROOT_DIR . "/public/";
        $n = '';
        if (!empty($GLOBALS['user']))
            $n = $GLOBALS['user']->userid;
        $n .= time() . rand() . "." . $ext;
        $ymd = date("Ymd");
        $save_path = "/data/" . $ymd . "/";
        if (!file_exists($public_dir . $save_path)) {
            mkdir($public_dir . $save_path);
        }
        $n = $save_path . $n;
        move_uploaded_file($sourcename, $public_dir . $n);
        return $n;
    }

    public static function countalbumbycondition($condition) {
        $r = DB::LimitQuery(self::$albumtable, array("condition" => $condition, "one" => true, "select" => "count(*) as count"));
        return $r['count'];
    }

    public static function albumbycondition($condition, $offset, $size) {
        return DB::LimitQuery(self::$albumtable, array("condition" => $condition,
                    "offset" => $offset, "size" => $size, "order" => "ORDER BY albumid desc"));
    }

    public static function countphotobyalbumid($albumid) {
        $r = DB::LimitQuery(self::$phototable, array("condition" => array("albumid" => $albumid), "select" => "count(*) as count", "one" => true));
        return $r['count'];
    }

    public static function photobyalbumid($albumid, $offset, $size) {
        return DB::LimitQuery(self::$phototable, array("condition" => array("albumid" => $albumid), "offset" => $offset, "size" => $size, "order" => "ORDER BY photoid desc"));
    }

    public static function albumbyid($albumid) {
        return DB::LimitQuery(self::$albumtable, array("condition" => array("albumid" => $albumid), "one" => true));
    }

    public static function modalbum($albumid, $data) {
        return DB::Update(self::$albumtable, $albumid, $data, "albumid");
    }

    public static function modphoto($photoid, $data) {
        return DB::Update(self::$phototable, $photoid, $data, "photoid");
    }

    public static function genthumb($filename) {
        $pre_dir = ROOT_DIR . "public";
        list($width, $height) = getimagesize($pre_dir . $filename);
        $newwidth = 135;
        $newheight = 120;

        $thumb = imagecreatetruecolor($newwidth, $newheight);
        $source = imagecreatefromjpeg($pre_dir . $filename);

        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

        $thumbpath = $filename . "thumb." . pathinfo($filename, PATHINFO_EXTENSION);
        imagejpeg($thumb, $pre_dir . $thumbpath, 100);
        return $thumbpath;
    }

}

/* End of file Photo */






    